package core.pojos;

import core.oauth.ServiceFactory;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Admin
 */
public class Converter {

    public static List<ItemInfo> fromListDropbox(List<DropboxItem> items) {
        ArrayList<ItemInfo> list = new ArrayList<>();
        if (items != null) {
            for (DropboxItem item : items) {
                ItemInfo itemInfo = new ItemInfo();
                itemInfo.setStorageType(ServiceFactory.DROPBOX);
                itemInfo.setName(getFileName(item.path));
                itemInfo.setId(item.path);
                itemInfo.setDir(item.is_dir);
                itemInfo.setModifiedDate(item.modified);
                if (!item.is_dir) {
                    itemInfo.setFileSize(item.bytes);
                    itemInfo.setFileExtension(getFileExtension(item.path));
                }
                list.add(itemInfo);
            }
        }
        return list;
    }

    public static List<ItemInfo> fromListDrive(List<DriveItem> items) {
        ArrayList<ItemInfo> list = new ArrayList<>();
        if (items != null) {
            for (DriveItem item : items) {
                ItemInfo itemInfo = new ItemInfo();
                itemInfo.setStorageType(ServiceFactory.DRIVE);
                itemInfo.setName(item.title);
                itemInfo.setId(item.id);
                itemInfo.setDir(item.isDir());
                itemInfo.setModifiedDate(item.modifiedDate);
                if (!itemInfo.isDir()) {
                    itemInfo.setFileSize(item.fileSize);
                    if (item.fileExtension != null) {
                        itemInfo.setFileExtension(item.fileExtension);
                    }
                }
                list.add(itemInfo);
            }
        }
        return list;
    }
    
    public static List<ItemInfo> fromListOneDrive(List<OneDriveItem> items) {
        ArrayList<ItemInfo> list = new ArrayList<>();
        if (items != null) {
            for (OneDriveItem item : items) {
                ItemInfo itemInfo = new ItemInfo();
                itemInfo.setStorageType(ServiceFactory.DRIVE);
                itemInfo.setName(item.name);
                itemInfo.setId(item.id);
                itemInfo.setDir(item.isDir());
                itemInfo.setModifiedDate(item.updated_time);
                if (!itemInfo.isDir()) {
                    itemInfo.setFileSize(item.size);
                }
                list.add(itemInfo);
            }
        }
        return list;
    }

    public static List<ItemInfo> fromDir(DriveDir dir) {
        return fromListDrive(dir.items);
    }

    public static List<ItemInfo> fromDir(DropboxDir dir) {
        return fromListDropbox(dir.items);
    }

    private static String getFileName(String path) {
        int index = path.lastIndexOf("/");
        return path.substring(index + 1);
    }

    private static String getFileExtension(String fileName) {
        int index = fileName.lastIndexOf(".");
        if (index < 0) {
            return "";
        }
        return fileName.substring(index + 1);
    }
}
